<script setup>
import { storeToRefs } from "pinia";
import { userStore } from "@/store/user";
import { ref } from "vue";
import {
  getLikelist,
  getMusicDetailList,
  getPlayListAll,
  getUserlist,
} from "../../apis/user";
import { createWindow, ipcRendererSend } from "../../utils/ipcRenderer";
import { Local } from "../../utils/storage";
import { musicStore } from "@/store/music";
import { useRouter } from "vue-router";
import emitter from "../../utils/eventBus";
const { loginStatus, userInfo, userId } = storeToRefs(userStore());
const { songListIds, musicDetail, songId, likeIdList } = storeToRefs(
  musicStore()
);
const router = useRouter();

const likeListId = ref([]);
const playList = ref([]);

//获取喜欢歌曲数量
const loveId = ref();
const getInit = async () => {
  await getUserlist({ uid: userId.value }).then((res) => {
    if (res.playlist.length > 0) {
      loveId.value = res.playlist[0].id;
    } else {
      return;
    }
  });
  await getPlayListAll({ id: loveId.value }).then((res) => {
    playList.value = res.songs;
  });

  // let queryIds = "";
  // await getLikelist({ uid: userId.value }).then((res) => {
  //   if (!res.ids) {
  //     playList.value = [];
  //     return;
  //   }
  //   likeListId.value = res.ids;
  //   likeIdList.value = res.ids;
  //   queryIds = res.ids.join(",");
  // });
  // await getMusicDetailList({
  //   ids: queryIds,
  // }).then((res) => {
  //   playList.value = res.songs;
  // });
};

getInit();

emitter.on("updateUser", () => {
  getInit();
});

const handlePlay = (item) => {
  musicDetail.value = item;
  songListIds.value = playList.value;
  Local.set("songList", playList.value);
  Local.set("musicInfo", item);
  songId.value = item.id;
  ipcRendererSend("windows-param", {
    emit: "playMusic",
    data: item.id,
    targetName: "home",
  });
};

const handleSinger = (id) => {
  createWindow({
    windowName: "singerDetail",
    path: "singerDetail",
    width: 1200,
    height: 820,
    param: { id: id },
  });
};

const allPlays = () => {
  songListIds.value = playList.value;
  Local.set("songList", playList.value);
  Local.set("musicInfo", playList.value[0]);
  songId.value = playList.value[0].id;
  ipcRendererSend("windows-param", {
    emit: "playMusic",
    data: playList.value[0].id,
    targetName: "home",
  });
};

const toLikeList = () => {
  router.push("/userLike");
};
</script>

<template>
  <div v-if="userInfo" class="musiclike_container">
    <div class="userinfo">
      <div class="cover">
        <el-image :src="userInfo.avatarUrl" fit="cover"></el-image>
      </div>
      <div class="user_name">{{ userInfo.nickname }}的音乐库</div>
    </div>
    <div class="like_list">
      <div @click="toLikeList" class="box">
        <div class="content">
          如若暮朝懂卿年，不负如来不负卿 <br />
          <br />
          闲看庭前繁华落，几缕东风无力弱
        </div>
        <div class="scr">
          <div class="left">
            <div class="info">我喜欢的音乐</div>
            <div class="count">{{ likeListId.length }}首歌</div>
          </div>
          <div class="right f-c-c">
            <i @click.stop="allPlays" class="iconfont icon-bofang"></i>
          </div>
        </div>
      </div>
      <div v-if="playList.length > 0" class="list">
        <div
          @dblclick="handlePlay(item)"
          v-for="item in playList.slice(0, 12)"
          :key="item.id"
          class="item"
          :class="{ active_position: item.id === songId }"
        >
          <div class="left">
            <el-image
              lazy
              style="width: 35px; height: 35px; border-radius: 5px"
              :src="item.al.picUrl + `${'?param=100y100'}}`"
              fit="cover"
            ></el-image>
          </div>
          <div class="right">
            <div class="music_name">{{ item.name }}</div>
            <span
              @click="handleSinger(item1.id)"
              v-for="(item1, index1) in item.ar"
              :key="index1"
              class="singer_name"
              >{{ item1.name }}
            </span>
          </div>
        </div>
      </div>
      <el-skeleton v-else animated style="margin-left: 30px" :rows="4">
      </el-skeleton>
    </div>
  </div>
</template>

<style scoped lang="scss">
.musiclike_container {
  .userinfo {
    display: flex;
    align-items: center;
    padding: 20px 0;
    .cover {
      width: 45px;
      height: 45px;
      .el-image {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .user_name {
      margin-left: 20px;
      font-size: 40px;
      font-weight: 600;
    }
  }
  .like_list {
    cursor: pointer;
    padding: 10px 0;
    display: flex;
    .box {
      width: 25vw;
      height: 190px;
      background-color: #eaeffd;
      border-radius: 15px;
      padding: 20px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .content {
        font-size: 20px;
        font-weight: 600;
        color: #335eea;
      }
      .scr {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left {
          .info {
            font-size: 22px;
            font-weight: 600;
            color: #335eea;
          }
          .count {
            font-size: 16px;
            color: #335eea;
          }
        }
        .right {
          width: 23px;
          height: 23px;
          background-color: #335eea;
          padding: 7px;
          border-radius: 50%;
          transition: all 0.2s;
          &:hover {
            transform: scale(1.01);
          }
          &:active {
            transform: scale(0.8);
          }
          i {
            font-size: 18px;
            color: #fff;
            background-color: #335eea;
            z-index: 555;
          }
        }
      }
    }
    .list {
      flex: 1;
      //   background-color: #ddd;
      margin-left: 30px;
      //   padding: 7px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      align-items: center;
      .item {
        cursor: pointer;
        display: flex;
        align-items: center;
        width: 30%;
        background-color: #fff;
        padding: 5px;
        border-radius: 10px;
        z-index: 44;
        &:hover {
          background-color: #f2f2f4;
        }

        .left {
        }
        .right {
          margin-left: 13px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          .music_name {
            font-size: 16px;
            font-weight: 600;
          }
          .singer_name {
            color: rgb(100, 89, 89);
            font-size: 12px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }
      .active_position {
        color: #335eea;
        background: #eaeffd;
        .info {
          .name,
          .creator_name {
            color: #335eea;
          }
        }
        .album,
        .timer {
          color: #335eea;
        }
      }
    }
  }
}
</style>
